﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalExam
{
    public partial class Conn : Form
    {
        public Conn()
        {
            InitializeComponent();
        }

        public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void Conn_Load(object sender, EventArgs e)
        {
            var use = "select * from Users";

            var sql = "select*from Articles";
            var dt = DbHelp.GetDataTable(sql);

            
            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        //查找
        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql =string.Format( "select * from Articles where Author like '%{0}%'",name);

            var dt = DbHelp.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }
        //添加
        private void button1_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            var res=form3.ShowDialog();

            if(res==DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

                var dt = DbHelp.GetDataTable(sql);

                dataGridView1.DataSource = dt;
            }

        }
        //更新
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var people = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var t = (int)dataGridView1.SelectedRows[0].Cells["CreateTime"].Value;

                

                Form3 form3 = new Form3(id,name,people,t);

                var res=form3.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var art = "select*from Articles";
                    var dt = DbHelp.GetDataTable(art);


                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("");
            }
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);



                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DbHelp.AddOrUpdateOrDelete(sql);

                    if (rowCount > 0)
                    {

                        var name = textBox1.Text;
                        var art = string.Format("select * from Articles where Author like '%{0}%'", name);

                        var dt = DbHelp.GetDataTable(art);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }

            else
            {
                MessageBox.Show("选择行","提示");
            }
            
        }
    }
}
